﻿using System.Linq;
using BloggerAutoPoster.Common.Mvvm;
using BloggerAutoPoster.Common.Toolkit;
using BloggerAutoPoster.Infrastructure.Models;
using BloggerAutoPosterService.Engine.Core;
using BloggerAutoPosterService.Engine.Utils;

namespace BloggerAutoPoster.Infrastructure.ViewModels
{
    public class ConfigViewModel
    {
        #region Constructors (1)

        public ConfigViewModel()
        {
            setupItemsData();
            setupCommands();
        }

        #endregion Constructors

        #region Properties (3)

        public ConfigUi ConfigUiData { set; get; }

        public DelegateCommand<object> DoApplyConfig { set; get; }

        public DelegateCommand<object> DoCloseConfig { set; get; }

        #endregion Properties

        #region Methods (4)

        // Private Methods (4) 

        void doApplyConfig(object data)
        {
            ConfigManager.SetConfigData("BlogUrl", ConfigUiData.BlogUrl);
            ConfigManager.SetConfigData("FeedToParse", ConfigUiData.FeedToParse);
            ConfigManager.SetConfigData("GooglePlusUserId", ConfigUiData.GooglePlusUserId);
            ConfigManager.SetConfigData("IsProxyEnabled", ConfigUiData.IsProxyEnabled ? "true" : "false");
            ConfigManager.SetConfigData("Password", ConfigUiData.Password);
            ConfigManager.SetConfigData("PostAt", ConfigUiData.PostAt);
            ConfigManager.SetConfigData("ProxyServerAddress", ConfigUiData.ProxyServerAddress);
            ConfigManager.SetConfigData("ProxyServerPassword", ConfigUiData.ProxyServerPassword);
            ConfigManager.SetConfigData("ProxyServerPort", ConfigUiData.ProxyServerPort.ToString());
            ConfigManager.SetConfigData("ProxyServerUserName", ConfigUiData.ProxyServerUserName);
            ConfigManager.SetConfigData("ReadSitesDataIntervalMin", ConfigUiData.ReadSitesDataIntervalMin.ToString());
            ConfigManager.SetConfigData("Tag", ConfigUiData.Tag);
            ConfigManager.SetConfigData("Title", ConfigUiData.Title);
            ConfigManager.SetConfigData("UsePersianDate", ConfigUiData.UsePersianDate ? "true" : "false");
            ConfigManager.SetConfigData("UserName", ConfigUiData.UserName);

            if (ConfigUiData.RunAtStartup)
                RunOnWindowsStartup.Do();
            else
                RunOnWindowsStartup.Undo();

            App.Messenger.NotifyColleagues("doCloseCfgView");
        }

        static void doCloseConfig(object data)
        {
            App.Messenger.NotifyColleagues("doCloseCfgView");
        }

        private void setupCommands()
        {
            DoApplyConfig = new DelegateCommand<object>(doApplyConfig, obj => true);
            DoCloseConfig = new DelegateCommand<object>(doCloseConfig, obj => true);
        }

        private void setupItemsData()
        {
            var serviceConfig = new ServiceConfig(useApplicationPath: true);
            ConfigUiData = new ConfigUi
            {
                BlogUrl = serviceConfig.BlogUrl,
                FeedToParse = serviceConfig.FeedToParse.Aggregate((s1, s2) => s1 + s2),
                GooglePlusUserId = serviceConfig.GooglePlusUserId.Aggregate((s1, s2) => s1 + s2),
                IsProxyEnabled = serviceConfig.IsProxyEnabled,
                Password = serviceConfig.Password,
                PostAt = serviceConfig.PostAt,
                ProxyServerAddress = serviceConfig.ProxyServerAddress,
                ProxyServerPassword = serviceConfig.ProxyServerPassword,
                ProxyServerPort = serviceConfig.ProxyServerPort,
                ProxyServerUserName = serviceConfig.ProxyServerUserName,
                ReadSitesDataIntervalMin = serviceConfig.ReadSitesDataIntervalMin,
                RunAtStartup = RunOnWindowsStartup.IsEnabled(),
                Tag = serviceConfig.Tag,
                Title = serviceConfig.Title,
                UsePersianDate = serviceConfig.UsePersianDate,
                UserName = serviceConfig.UserName
            };
        }

        #endregion Methods
    }
}
